//
//  PhotoDetailView.m
//  DayDay
//
//  Created by wade on 16/5/16.
//  Copyright © 2016年 wade. All rights reserved.
//

#import "PhotoDetailView.h"
#import "DetailCell.h"
#import "DetailAdCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "BaiduAdView.h"

@interface PhotoDetailView ()
{
    UIButton *rightbtn;
    DetailAdCell *adcell;
}
@end

@implementation PhotoDetailView

static NSString * const reuseIdentifier = @"DetailCell";
static NSString * const adreuseIdentifier = @"DetailAdCell";
-(void)rightaction
{
    [SVProgressHUD show];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    [parameter setValue:_imgid forKey:@"id"];
    
    [manager POST:@"http://www.shgs123.com/api/likeImgList.php" parameters:parameter
         progress:^(NSProgress * _Nonnull downloadProgress)
     {
         //NSLog(@"%lld", downloadProgress.totalUnitCount);
     }
          success:^(NSURLSessionDataTask *operation, id responseObject)
     {
         
         [rightbtn setTitle:@"已点赞" forState:UIControlStateNormal];
         [SVProgressHUD dismiss];
     }
          failure:^(NSURLSessionDataTask *operation, NSError *error)
     {
         NSLog(@"%@",error);
         [SVProgressHUD dismiss];
         
     }];

    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightbtn.frame = CGRectMake(0, 0, 80, 35);
    [rightbtn setTitle:@"点赞" forState:UIControlStateNormal];
    [rightbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightbtn addTarget:self action:@selector(rightaction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightbtn];
    
    
   
   
    //百度插屏
    
    
    
}
-(void)viewDidAppear:(BOOL)animated
{
    //百度banner
    BaiduAdView *kk = [[BaiduAdView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 48)];
    [self.sharedAdView addSubview:kk];
}
-(void)loadinterstitialView
{
    //百度插屏
//    BaiduAdInterstitialVC *ii = [[BaiduAdInterstitialVC alloc] init];
//    ii.view.frame = self.myCollection.frame;
//    [adcell addSubview:ii.view];

    BaiduAdInterstitialView *aiv = [[BaiduAdInterstitialView alloc] initWithFrame:self.view.frame];
    [aiv setSuccessBlock:^(){
        
        [aiv.interstitialView presentFromView:self.view];
        
    }];
    
    [adcell addSubview:aiv];
    
   
    
    
    if (self.interstitialView.isReady){
        [self.interstitialView presentFromRootViewController:self];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width , self.view.frame.size.height - 49 - 64);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    //#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //#warning Incomplete implementation, return the number of items
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailCell *cell;
    
    
    if(indexPath.row == 1)
    {
        adcell = [collectionView dequeueReusableCellWithReuseIdentifier:adreuseIdentifier forIndexPath:indexPath];
        
        if(!adcell.isLoad)
        {
            [self loadinterstitialView];
            
            adcell.isLoad = YES;
        }
        
        return adcell;
    }
    else
    {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        
        cell.imgUrl = self.imgUrl;
        [cell.ImgView sd_setImageWithURL:[NSURL URLWithString:self.imgUrl] placeholderImage:nil];
        
        return cell;
    }
    
    
    
    
    return cell;
}


@end
